/**
 * [40] 组合总和 II
 *
 * @format
 * @lc app=leetcode.cn id=40 lang=javascript
 */

// @lc code=start
/**
 * @param {number[]} candidates
 * @param {number} target
 * @return {number[][]}
 */
var combinationSum2 = function (candidates, target) {
    candidates.sort((a, b) => a - b)
    // const [result, used] = [[], {}]
    const result = []
    let [rest, last] = [0, null]
    for (let i = 0; i < candidates.length; i++) {
        if (candidates[i] == last) continue
        else last = candidates[i]
        // if (used[candidates[i]]) continue
        // else used[candidates[i]] = true
        rest = target - candidates[i]
        if (rest == 0) result.push([candidates[i]])
        else if (rest > 0) {
            combinationSum2(candidates.slice(i + 1), rest).forEach((e) => {
                e.push(candidates[i])
                result.push(e)
            })
        }
    }
    return result
}
// @lc code=end
